/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_DBBrowse.
	/// </summary>
	public class Block_DBBrowse : Block
	{

		public enum BROWSE_TYPE
		{
			FirstRecord = 1,
			LastRecord = 2,
			NextRecord = 3,
			PrevRecord = 4,
			CurrentRecord = 5
		}

		private BROWSE_TYPE _browseType;
		private string _recordSet;

		public override TYPE GetBlockType()
		{
			return TYPE.DBBrowse;
		}

		/// <summary>
		/// This block is bigger than normal, return bigger size.
		/// </summary>
		/// <returns></returns>
		public override int GetWidth()
		{
			return 128;
		}


		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_DBBrowse::GetXML Started.");	
			// <DbBrowse name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<DbBrowse name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			// ...recordset="RECORDSET"...
			xml += " recordset=\"" + Utils.XmlString(_recordSet) + "\"";

			string inputName = "whops.";
			// ...first="FIRST"...
			if (_browseType == BROWSE_TYPE.FirstRecord)
			{
				xml += " first=\"1\"";
				inputName = "First";
			}
			else
			{
				xml += " first=\"0\"";
			}
			// ...last="LAST"...
			if (_browseType == BROWSE_TYPE.LastRecord)
			{
				xml += " last=\"1\"";
				inputName = "Last";
			}
			else
			{
				xml += " last=\"0\"";
			}
			// ...next="NEXT"...
			if (_browseType == BROWSE_TYPE.NextRecord)
			{
				xml += " next=\"1\"";
				inputName = "Next";
			}
			else
			{
				xml += " next=\"0\"";
			}
			// ...prev="PREV"...
			if (_browseType == BROWSE_TYPE.PrevRecord)
			{
				xml += " prev=\"1\"";
				inputName = "Prev";
			}
			else
			{
				xml += " prev=\"0\"";
			}
			// ...current="CURRENT"...
			if (_browseType == BROWSE_TYPE.CurrentRecord)
			{
				xml += " current=\"1\"";
				inputName = "Current";
			}
			else
			{
				xml += " current=\"0\"";
			}

			// End the header xml-part.
			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId, inputName);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </DbBrowse>
			xml +="\t\t\t</DbBrowse>\r\n";
			return xml;
		}

		public Block_DBBrowse(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			Trace.Debug("Block_DBBrowse created.");

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.BOF, 7));
			outputErrorIds.Add(new BlockOutput(OUTPUT.EOF_2, 6));
			outputErrorIds.Add(new BlockOutput(OUTPUT.DbFailed, 36));
			outputErrorIds.Add(new BlockOutput(OUTPUT.DbTimeout, 34));
			outputErrorIds.Add(new BlockOutput(OUTPUT.SQLFailed, 168));

			_browseType = (BROWSE_TYPE)rawData[_currentPos + 4];

			_currentPos += 8;

			// recordset name
			_recordSet = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			Trace.Debug("Block_DBBrowse: type: " + _browseType +
				", RecordSet: " + _recordSet);
		}
	}
}